package com.yxw.live_vod_boot.utils;

import com.yxw.live_vod_boot.vo.UserTokenVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Administrator
 */
@Component
public class UserUtil {

    private final HttpServletRequest request;
    @Autowired
    public UserUtil(HttpServletRequest request){
        this.request = request;
    }

    public UserTokenVo getLoginUserVo(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            return JwtUtil.parseToken(token);
        }
        return null;
    }
    public Long getLoginUserId(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            return yxUserTokenVo == null ? null : yxUserTokenVo.getUserId();
        }
        return null;
    }

    public String getLoginName(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            return yxUserTokenVo == null ? null : yxUserTokenVo.getNickName();
        }
        return null;
    }

    public String getLoginMobilePhone(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            return yxUserTokenVo == null ? null : yxUserTokenVo.getMobilePhone();
        }
        return null;
    }

    public String getLoginUserName(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            return yxUserTokenVo == null ? null : yxUserTokenVo.getUserName();
        }
        return null;
    }
}
